﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1025HomeWork
{
    
    public partial class Form1 : Form
    {
        public Random rnd = new Random();
        public int Width;
        public int height;
        //接口类型的列表
        public List<IMoveable> iF_List = new List<IMoveable>();
        //基类类型的列表
        public List<Animal> animalList = new List<Animal>();
        public List<Vehicle> vehicleList = new List<Vehicle>();
        public int[,] square;
        public char[,] squareName;
        public Form1()
        {
            InitializeComponent();
        }
        public void SquareClass(int _w = 5, int _h = 5) //默认]参数
        {
            Width = _w; height = _h;
            //创建存放场地的二维数组
            square = new int[Width, height];
            squareName = new char[Width, height];
            //创建场地上的动物和汽车
            IMoveable blackDog = new BlackDog(0, 1);
            IMoveable whiteDog = new WhiteDog(3, 2);
            IMoveable bigCattle = new BigCattle(4, 1);
            IMoveable smallCattle = new SmallCattle(1, 4);
            IMoveable truck = new Truck(2, 3);
            IMoveable car = new Car(4, 4);
            IMoveable suv = new SUVClass(2, 2);
            squareName[0, 1] = 'B';
            squareName[3, 2] = 'W';
            squareName[4, 1] = 'N';
            squareName[1, 4] = 'n';
            squareName[2, 3] = 'T';
            squareName[4, 4] = 'C';
            squareName[2, 2] = 'S';
            //将对象加入“接口引用”列表
            iF_List.Add(blackDog);
            iF_List.Add(whiteDog);
            iF_List.Add(bigCattle);
            iF_List.Add(smallCattle);
            iF_List.Add(truck);
            iF_List.Add(car);
            iF_List.Add(suv);
            //将对象加入“基类引用”列表，
            //注意没有新建对象，只是作强制转换后，向列表中添加引用。
            animalList.Add((Animal)blackDog);
            animalList.Add((Animal)whiteDog);
            animalList.Add((Animal)bigCattle);
            animalList.Add((Animal)smallCattle);
            //将对象加入依据基类引用创建列表
            vehicleList.Add((Vehicle)truck);
            vehicleList.Add((Vehicle)car);
            vehicleList.Add((Vehicle)suv);
        }
        
        public void ClearFlags()//清空场地上所有标记
        {
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    square[i, j] = 0;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            ClearFlags();//清空场地上所有标记

            foreach(Animal an in animalList)
            {
                an.X = an.X + rnd.Next(0, 2);
                an.Y = an.Y + rnd.Next(0, 2);
                if (an.X > Width-1) an.X = an.X - 5;
                if (an.Y > height-1) an.Y = an.Y - 5;
                square[an.X, an.Y]++;
                if (square[an.X, an.Y] == 1)
                {
                    if (an.aName == "黑狗")
                    {
                        squareName[an.X, an.Y] = 'B';
                    }
                    else if (an.aName == "白狗")
                    {
                        squareName[an.X, an.Y] = 'W';
                    }
                    else if (an.aName == "大牛")
                    {
                        squareName[an.X, an.Y] = 'N';
                    }
                    else if (an.aName == "小牛")
                    {
                        squareName[an.X, an.Y] = 'n';
                    }
                }
            }
            foreach (Vehicle vh in vehicleList)
            {
                vh.X = vh.X + rnd.Next(0, 2) * 2;
                vh.Y = vh.Y + rnd.Next(0, 2) * 2;
                if (vh.X > Width-1) vh.X = vh.X - 5;
                if (vh.Y > height-1) vh.Y = vh.Y - 5;
                square[vh.X, vh.Y]++;
                if (square[vh.X, vh.Y] == 1)
                {
                    if (vh.aName == "卡车")
                    {
                        squareName[vh.X, vh.Y] = 'T';
                    }
                    else if (vh.aName == "轿车")
                    {
                        squareName[vh.X, vh.Y] = 'C';
                    }
                    else if (vh.aName == "越野车")
                    {
                        squareName[vh.X, vh.Y] = 'S';
                    }
                }
            }
            pictureBox1.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics g = e.Graphics;
            string title = "  "; //场地的各列上方的编号
            for (int i = 0; i < height; i++) { title += i + " "; }
            Font font = new Font("黑体", 20);
            g.DrawString(title , font, new SolidBrush(Color.Red), new Point(20, 50));

            string splitter = "  "; //分割线
            for (int i = 0; i < height; i++) { splitter += "- "; }
            g.DrawString(splitter, font, new SolidBrush(Color.Red), new Point(20, 65));

            for (int i = 0; i < Width; i++)
            {
                g.DrawString(i + "|", font, new SolidBrush(Color.Red), new Point(10, 80 + i * 35));
                Console.Write(i + "|");  //场地的各行左侧的编号和分割线
                for (int j = 0; j < height; j++)
                {
                    g.DrawString("□", font, new SolidBrush(Color.Black), new Point(40 + i * 30, 80 + j * 35));
                    if (square[i, j] == 1)
                        g.DrawString(squareName[i, j].ToString(), font, new SolidBrush(Color.Green), new Point(45 + i * 30, 80 + j * 35));
                    else if (square[i, j] == 2)
                    {
                        g.DrawString("2", font, new SolidBrush(Color.Blue), new Point(40 + j * 30, 80 + j * 35));
                    }

                }

            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SquareClass(5,5);
            //显示开始时的场地
            //如果栅格中有动物或汽车，该栅格保存对象的数量
            foreach (Animal an in animalList) square[an.X, an.Y]++;
            foreach (Vehicle vh in vehicleList) square[vh.X, vh.Y]++;


        }
    }
}
